//
//  AppDelegate.swift
//  Weibo
//
//  Created by apple on 2016/11/29.
//  Copyright © 2016年 yanjun. All rights reserved.
//

import UIKit

import UserNotifications

import SVProgressHUD

import AFNetworking


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // 程序额外设置
        setupAdditions()
        
        
        
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()

        loadAppInfo()
        
        return true
    }

    


}
// MARK: - 设置额外信息
extension AppDelegate
{
    
    fileprivate func setupAdditions(){
        
        // 设置 SVProgressHUD 最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        // 设置网络家加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        // 设置用户授权显示通知
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.carPlay,.badge], completionHandler: { (success,error) in
                
                print("授权" + (success ? "成功" : "失败"))
                
            })
        } else {
            // 取得用户授权显示通知[上方的提示条/声音/badgeNumber]
            let notifySettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }

    }
    
}


// MARK: - 从服务器加载应用程序信息
extension AppDelegate {
    
    fileprivate func loadAppInfo() {
        
        DispatchQueue.global().async {
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            let data = NSData(contentsOf: url!)
            
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            let jsonPath = (docDir as NSString).appendingPathComponent("main,json")
            
            // 直接保存在沙盒，等待下次程序启动使用
            data?.write(toFile: jsonPath, atomically: true)
            
            
        }
        
    }
    
    
}

